﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for AssignDriver.xaml
    /// </summary>
    public partial class AssignDriver : Window
    {
        public AssignDriver()
        {
            InitializeComponent();
            populateactivityDataGrid();
        }
        
        public List<COMSERV_ACTIVITY> activities
        {
            get;
            set;
        }

        public List<COMSERV_ACTIVITY> dpactivity
        {
            get;
            set;
        }

        public List<Driver> drivers
        {
            get;
            set;
        }
        private static string getTerm()
        {
            DataClassesDataContext db = new DataClassesDataContext();

            int term = getTermID();

            if (term != 0)
            {

                IQueryable<COMSERV_SEMESTER_YEAR> info = from c in db.COMSERV_SEMESTER_YEARs
                                                         where c.semester_yearid == (int)term
                                                         select c;
                info.ToList();
                return info.First().semester + " " + info.First().year;
            }
            return "Please generate events first!";
        }

        private static int getTermID()
        {
            DataClassesDataContext db = new DataClassesDataContext();

            try
            {
                int id = (int)(from e in db.Events
                               join a in db.COMSERV_ACTIVITies on (int)e.ServiceID equals (int)a.activityid
                               select a.semester_yearid).FirstOrDefault();
                return id;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return 0;
        }

        private void populateactivityDataGrid()
        {
            DataClassesDataContext db = new DataClassesDataContext();

            IQueryable<COMSERV_ACTIVITY> activities = from c in db.COMSERV_ACTIVITies
                                                      where c.semester_yearid == getTermID() && c.needed != "One-time Project" && c.transportation == 1
                                                      select c;

            activityDataGrid.ItemsSource = activities;

        }

        private void activityDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataClassesDataContext db = new DataClassesDataContext();

           
            IQueryable<Driver> drivers = from t in db.Drivers
                                         join x in db.driver_preferences on t.ID equals x.driver_id
                                         where x.preference_id == ((COMSERV_ACTIVITY)(activityDataGrid.SelectedItem)).activityid
                                        select t;

            if (drivers.Count() == 0)
            {
                string message = "No drivers chose this activity. Displaying all.";
                string caption = "Drivers";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Exclamation);
                drivers = from z in db.Drivers select z;
            }
            driverDataGrid.ItemsSource = drivers;


            //fix or make better
            IQueryable<Event> events = (from z in db.Events where z.ServiceID == ((COMSERV_ACTIVITY)(activityDataGrid.SelectedItem)).activityid select z).Distinct();
            
          
            List<Driver> assigneddrivers = (from g in db.Drivers where g.ID == events.First().DriverID select g).ToList();

            if (assigneddrivers.Count() > 0)
            {
                assignedDriverLabel.Content = assigneddrivers.First().first_name + " " + assigneddrivers.First().last_name;
            }
            else
            {
                assignedDriverLabel.Content = " No Driver ";
            }
            
            //-------------
        }

        private void driverDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (driverDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();

                IQueryable<driver_preference> prefs = from b in db.driver_preferences where b.driver_id == ((Driver)(driverDataGrid.SelectedItem)).ID select b;

                IQueryable<COMSERV_ACTIVITY> dpactivity = from t in prefs
                                                          join x in db.COMSERV_ACTIVITies on t.preference_id equals x.activityid
                                                          select x;

                driverPreferenceDataGrid.ItemsSource = dpactivity;
            }
        }

        private void assignButton_Click(object sender, RoutedEventArgs e)
        {
            if (activityDataGrid.SelectedItem != null && driverDataGrid.SelectedItem != null)
            {
                //--Display message---------------------------------------
                string activity = ((COMSERV_ACTIVITY)(activityDataGrid.SelectedItem)).activity_name;
                string driver = ((Driver)(driverDataGrid.SelectedItem)).first_name;
                string message = "This will assign " + activity + " with " + driver + " as the driver" ;
                string caption = "Vehicle";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Question);
                //---------------------------------------------------------
                DataClassesDataContext db = new DataClassesDataContext();
                int driverid = ((Driver)(driverDataGrid.SelectedItem)).ID;
                IQueryable<Event> events = from c in db.Events where c.ServiceID == ((COMSERV_ACTIVITY)(activityDataGrid.SelectedItem)).activityid select c;

                List<Event> eventslist = events.ToList();

                foreach (Event d in eventslist)
                {
                    assignDriver(d, driverid);
                }

                assignedDriverLabel.Content = ((Driver)(driverDataGrid.SelectedItem)).first_name + " " + ((Driver)(driverDataGrid.SelectedItem)).last_name;

                driverDataGrid.SelectedItem = null;

                
               

            }
            else
            {
                string message = "Please select both an Activity and a Driver";
                string caption = "Assign";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Exclamation);
            }
        }

        private void assignDriver(Event e, int v)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            // Query the database for the row to be updated.
            var query =
                from ev in db.Events
                where ev.ID == e.ID
                select ev;
            // Execute the query, and change the column values
            // you want to change.
            foreach (Event ev in query)
            {
                ev.DriverID = v;
                // Insert any additional changes to column values.
            }

            // Submit the changes to the database.
            try
            {
                db.SubmitChanges();
            }
            catch (Exception w)
            {
                Console.WriteLine(w);
                // Provide for exceptions.
            }
        }


    
    }
}
